package com.maven.plugins.mybatis;

import java.io.File;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

import com.maven.plugins.mybatis.utils.GeneratorUtils;

/**
 * mybatis MoJo
 * 
 * @author : fredia
 * @email : trumpey@163.com
 * @since : 2017年11月23日
 * @version : v0.0.1
 */
@Mojo(name = "mybatis")
public class GeneratorMojo extends AbstractMojo {

	@Parameter(defaultValue = "${project.build.directory}", property = "outputDir", required = true)
	private String mapper;

	@Parameter(defaultValue = "${project.build.directory}", property = "outputDir", required = true)
	private String sqlmap;

	@Parameter(defaultValue = "${project.build.directory}", property = "outputDir", required = true)
	private String vo;

	@Parameter(defaultValue = "${project.build.directory}", property = "outputDir", required = true)
	private String configDirectory;
	
	//TODO 自行扩展参数
	
	
	public void execute() throws MojoExecutionException {
		File context = new File(this.configDirectory);
		if ((context.isDirectory()) || (!context.exists())) {
			getLog().error(new Exception("configDirectory:" + this.configDirectory + ",必须是一个文件"));

			return;
		}
		File mapperFile = new File(this.mapper);
		if (!mapperFile.isDirectory()) {
			getLog().error(new Exception("mapper:" + this.mapper + ",必须是一个文件夹"));
			return;
		}

		File sqlmapFile = new File(this.sqlmap);
		if (!sqlmapFile.isDirectory()) {
			getLog().error(new Exception("sqlmap:" + this.sqlmap + ",必须是一个文件夹"));
			return;
		}

		File voFile = new File(this.vo);
		if (!voFile.isDirectory()) {
			getLog().error(new Exception("vo:" + this.vo + ",必须是一个文件夹"));
			return;
		}
		try {
			GeneratorUtils.runner(this.mapper, this.sqlmap, this.vo, this.configDirectory);
		} catch (Exception e) {
			getLog().error(e);
		}
	}
}
